/***************************************************************************
 *   Copyright (C) 2009 by Philipp Müller                                  *
 *   philipp.mueller@gmx.de                                                *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA          *
 ***************************************************************************/

#include <QtCore>
#include <QtGui>

#include <QMouseEvent>
#include <QPainter>

#include "widgetrecordingslider.h"


WidgetRecordingSlider::WidgetRecordingSlider( QWidget * parent ) : QWidget( parent )
{
    m_length     = 0;
    m_pos        = 0;
    m_currentEnd = 0;

    setMinimumSize ( 200, 0 );
}


WidgetRecordingSlider::~WidgetRecordingSlider()
{
}


void WidgetRecordingSlider::setLength( qint32 length )
{
    m_length = length;
    update();
}


void WidgetRecordingSlider::setPos( qint64 pos )
{
#ifdef NETWORK_DEBUG
    qDebug() << "WidgetRecordingSlider::setPos was called with new pos: " << pos;
#endif
    m_pos = pos / 1000;
    update();
}


void WidgetRecordingSlider::setCurrentEnd( qint32 end )
{
    m_currentEnd = end;
    update();
}


void WidgetRecordingSlider::updateView()
{
    update();
}


void WidgetRecordingSlider::mousePressEvent( QMouseEvent *event )
{
    if ( event->button() == Qt::LeftButton )
    {
        setPos( timeFromPixel( event->pos().x() ) * 1000 );
        emit newPos( m_pos );
        event->accept();
    }
    else
    {
        QWidget::mousePressEvent( event );
    }
}


void WidgetRecordingSlider::paintEvent( QPaintEvent * /*event*/ )
{
    QPainter painter( this );

    painter.save();

    QRect rect;

    //Background and frame
    rect = QRect( 0, 0, width() - 1, height() - 1 );
    painter.drawRect( rect );

    //Position
    rect = QRect( 1, 1, pixelFromTime( m_pos ), height() - 2 );
    painter.fillRect( rect, Qt::yellow );

    //Display end of current recording FIXME: Get this implemented
    if ( m_currentEnd != 0 )
    {
        rect = QRect( pixelFromTime( m_currentEnd ),
                    1,
                    pixelFromTime( m_length - m_currentEnd ),
                    height() - 2 );
        painter.fillRect( rect, Qt::red );
    }

    painter.restore();
}


qint32 WidgetRecordingSlider::timeFromPixel( int pixel )
{
    if ( width() < 3 )
    {
        return 0;
        qWarning() << "WidgetRecordingSlider::timeFromPixel width < 3:" << width();
    }

    double result = 0.5 + (double) pixel / ( width() - 2 ) * m_length;

#ifdef NETWORK_DEBUG
    qDebug() << "WidgetRecordingSlider::timeFromPixel result:" << result << "  width()" << width() << "  pixel:" << pixel;
#endif

    return result;
}


int WidgetRecordingSlider::pixelFromTime( qint32 time )
{
    if ( m_length == 0 )
    {
        return 0;
        qWarning() << "WidgetRecordingSlider::pixelFromTime m_length == 0";
    }


    double result = 0.5 + ( (double) time / m_length ) * ( width() - 2 );;

#ifdef NETWORK_DEBUG
    qDebug() << "WidgetRecordingSlider::pixelFromTime result:" << result << "  width()" << width() << "  time:" << time << "  length: " << m_length;
#endif

    return result;
}
